package cn.edu.ctbu.api01.restcontroller;
import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.helper.PageUtils;
import cn.edu.ctbu.api01.repository.StudentRepository;
import cn.edu.ctbu.api01.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "api/student")
public class StudentRestcontroller {
    @Autowired
    StudentService studentService;
    @GetMapping("/list")
    //获取页面参数
    public PageUtils findAll(Integer pageSize, Integer pageNum) {
        Pageable pageable=PageRequest.of(pageNum-1, pageSize);
        Page<Student> tempStudents=studentService.findAll(pageable);
        PageUtils pageUtils=new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return pageUtils;
    }
    @GetMapping("/edit/{id}")
    public String editStudent(@PathVariable Long id) {
        // 处理编辑学生的逻辑
        return "mymodel";  // 返回编辑学生的视图名称
    }
    /**
     * url:/api/student/get/id
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id) {
        Student student=studentService.findById(id);
        student.setPassword("");
        return student;
    }
    /* id查询  http://localhost:8080/api/student/2   2表示id */
    @GetMapping(value = "{id}")
    public Student GetById(@PathVariable Integer id) {
        return studentService.findById(id);
    }
    /* id删除  http://localhost:8080/api/student/delete?id=2   2表示id */
//    @DeleteMapping(value = "delete/{id}")
//    public boolean DeleteById(@PathVariable Integer id) {
//        studentService.deletebyid(id);
//        return true;
//    }
    /* id添加 */
//    @PostMapping(value = "add")
//    public Integer Add(@RequestBody Student student) {
//        studentRepository.save(student);
//        return student.getId();
//    }
    /* id更新修改 */
//    @PutMapping(value = "update")
//    public Integer Update(@RequestBody Student student) {
//        studentRepository.save(student);
//        return student.getId();
//    }
    /**
     * 新增学生，api/student/insert
     */
    @PostMapping("/insert")
//    public Student insert(Student student) {
//        Student student1=studentService.insert(student);
//        return student1;
//    }
    public int insert(@Valid Student student, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return -1;
        }
        studentService.insert(student);
        Integer id=student.getId();
        return id.intValue();
    }
    /**
     * 更新学生，api/student/update
     */
    @PutMapping("/update")
    public Student update(Student student) {
        //读取旧数据
        Student oldstudent=studentService.findById(student.getId());
        if(StringUtils.isEmpty(student.getPassword())){
            //没有新密码—>就用旧密码
            student.setPassword(oldstudent.getPassword());
        }
        student=studentService.update(student);
        return student;
    }
    /**
     * 删除学生，api/student/delete
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        studentService.deletebyid(id);
    }
    /**
     * 新增或更新学生，api/student/save
     */
    @PostMapping("/save")
    public Student saveOrUpdate(Student student) {
        if (student.getId() != null) {
            // 如果学生已存在，执行更新操作
            return studentService.update(student);
        } else {
            // 如果学生不存在，执行新增操作
            return studentService.insert(student);
        }
    }
    @GetMapping("/search")
    public List<Student> search(@RequestParam String term, @RequestParam String type) {
        List<Student> searchResults = studentService.searchStudentsByTerm(term,type);
        return searchResults;
    }
    @GetMapping(value = "/login")
    public Boolean login(String userName, String password) throws Exception{
        return studentService.validUserNameAndPassword(userName, password);
    }
}